#ifndef SERIALPORT_H_  
#define SERIALPORT_H_ 
#ifdef _WIN32
#include <Windows.h>  
#else
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#define HANDLE int
#define DWORD unsigned int
#define LPDCB void*
#define UINT unsigned int
#define UCHAR unsigned char
#endif

#pragma once
class SerialPort
{
public:
    SerialPort();
    ~SerialPort();
public:
    bool InitPort(const char* device, UINT baud = 9600, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = 0);
    bool InitPort(const char* device, const LPDCB& plDCB);
    bool WriteData(unsigned char* pData, unsigned int length);
    UINT GetBytesInCOM();
    bool ReadChar(UCHAR &cRecved);
private:
    bool openPort(const char* device);
    void ClosePort();
#ifdef _WIN32
    HANDLE  m_hComm; //串口句柄 
    static bool s_bExit; //线程退出标志变量 
    CRITICAL_SECTION   m_csCommunicationSync; //同步互斥,临界区保护
#else
    int m_hComm; //串口文件描述符
    struct termios m_oldtio; //原有串口配置
    pthread_mutex_t m_mutex; //互斥锁
#endif
};
#endif



